﻿// //------------------------------------------------------------------------------
// // <copyright file="" company="Dascoba Development">
// //  Copyright © Dascoba Development 2010
// // </copyright>
// //------------------------------------------------------------------------------
namespace Dascoba.Umb.FileManager.Support
{
    using System.Collections.Generic;
    using System.Text;

    using umbraco;
    using umbraco.BusinessLogic.Actions;
    using umbraco.interfaces;

    public class ContextMenu : List<IAction>
    {
        /// <summary>
        ///   Initializes a new instance of the <see cref = "ContextMenu" /> class.
        /// </summary>
        /// <param name = "menuId">The menu id.</param>
        /// <param name = "itemClass">The item class.</param>
        /// <param name = "skinClass">The skin class.</param>
        public ContextMenu(string menuId, string itemClass, string skinClass)
        {
            this.MenuId = menuId;
            this.ItemClass = itemClass;
            this.SkinClass = skinClass;
            this.FolderContentItems = new List<FolderContentItem>();
        }

        /// <summary>
        ///   Gets or sets the menu id.
        /// </summary>
        /// <value>The menu id.</value>
        public string MenuId { get; internal set; }

        /// <summary>
        ///   Gets or sets the CSS class.
        /// </summary>
        /// <value>The CSS class.</value>
        public string SkinClass { get; set; }

        /// <summary>
        ///   Gets or sets the item class.
        /// </summary>
        /// <value>The item class.</value>
        public string ItemClass { get; set; }

        /// <summary>
        ///   Returns a <see cref = "System.String" /> that represents this instance.
        /// </summary>
        /// <returns>
        ///   A <see cref = "System.String" /> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            var contextMenuList = new StringBuilder();
            contextMenuList.AppendFormat(@"<ul id=""{0}"" class=""jeegoocontext {1}"">", this.MenuId, this.SkinClass);
            foreach (var action in this)
            {
                contextMenuList.AppendLine();

                if (action.GetType() == typeof(ContextMenuSeperator))
                {
                    contextMenuList.Append(@"<li class=""separator""></li>");
                }
                else
                {
                    if (action.Icon.StartsWith("."))
                    {
                        contextMenuList.AppendFormat(
                            @"<li id=""{0}"" class=""icon""><span class=""{1}""></span>{2}</li>",
                            action.Alias.Replace(' ', '_').Trim(),
                            action.Icon.Replace(".", string.Empty),
                            ui.Text("actions", action.Alias));
                    }
                    else
                    {
                        contextMenuList.AppendFormat(
                            @"<li id=""{0}"" class=""icon""><span class=""icon""><img src=""{1}"" alt=""""/></span>{2}</li>",
                            action.Alias.Replace(' ', '_').Trim(),
                            action.Icon.Replace("/../..", "../.."),
                            ui.Text("actions", action.Alias));
                    }
                }
            }

            contextMenuList.AppendLine("</ul>");

            return contextMenuList.ToString();
        }

        /// <summary>
        ///   Gets or sets the folder content items.
        /// </summary>
        /// <value>The folder content items.</value>
        public List<FolderContentItem> FolderContentItems { get; private set; }
    }
}